using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Atechnology.glassCAD.Shapes
{
    [Serializable]
    public class ModelPoint : IEquatable<ModelPoint>, ICloneable
    {
        protected static double tolerance = 0.0000001;

        public static double Tolerance
        {
            get { return ModelPoint.tolerance; }
            set { ModelPoint.tolerance = value; }
        }

        protected double x;

        public virtual double X
        {
            get { return x; }
            set { x = value; this.Validate(); }
        }
        protected double y;

        public virtual double Y
        {
            get { return y; }
            set { y = value; this.Validate(); }
        }

        public ModelPoint()
        {
            this.Data = new List<object>();
        }

        public ModelPoint(double x, double y)
        {
            this.data = new List<object>();
            this.x = x;
            this.y = y;
        //    this.Validate();
        }

        //public static bool operator==(ModelPoint fst, ModelPoint sec)
        //{
        //    if (ModelCalculations.GetDistance(fst, sec) < ModelPoint.Tolerance)
        //        return true;
        //    else
        //        return false;   
        //}

        //public static bool operator !=(ModelPoint fst, ModelPoint sec)
        //{
        //    if (ModelCalculations.GetDistance(fst, sec) >= ModelPoint.Tolerance)
        //        return true;
        //    else
        //        return false;
        //}

        #region ICloneable Members

        public object Clone()
        {
            ModelPoint point = new ModelPoint();
            point.X = this.X;
            point.Y = this.Y;
            point.Data = this.Data;
            return point;
        }

        #endregion

        public void Validate()
        {
            //if (double.IsNaN(this.x) || double.IsNaN(this.y))
            //    Debugger.Break();

        }

        private List<object> data;

        public List<object> Data
        {
            get { return data; }
            set { data = value; }
        }

        public override int GetHashCode()
        {
            int x = this.x.GetHashCode();
            int y = this.y.GetHashCode();
        //    string hashString = x.ToString() + y.ToString();
        //    return hashString.GetHashCode();
            return x ^ y;
        }

        public bool Equals(ModelPoint other)
        {
            if (ModelCalculations.GetDistance(this, other) < 0.00001)
                return true;
            else
                return false;
        }


        public override bool Equals(object obj)
        {
            return this.Equals(obj as ModelPoint);
        }

        public override string ToString()
        {
            return x.ToString() + "; " + y.ToString();
        }
    }
}
